package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.feign.MyLoadBanlance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 赵新
 * @create 2021-10-25 14:28
 */
@RestController
@Slf4j
public class OrderController {
    private static  final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    public MyLoadBanlance myloadBanlance;
    @Resource
    public DiscoveryClient discoveryClient;
    @Resource
    public RestTemplate restTemplate;
    @GetMapping("/consumer/payment/create")
    public CommonResult<Integer> create(Payment payment){
        return  restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        System.out.println("1111111111");
        log.info("查询id="+id);
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

    @GetMapping("/consumer/payment/getEntity/{id}")
    public CommonResult<Payment> getEntity(@PathVariable("id")Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        log.info(entity.getHeaders()+"-----"+entity.getHeaders()+"-----"+entity.getStatusCode()+"-----"+entity.getStatusCodeValue());
        if(entity.getStatusCode()== HttpStatus.OK){
            return entity.getBody();
        }else {
            return new CommonResult<>(400,"未查询到数据");
        }
    }
    @GetMapping("/consumer/payment/postEntity")
    public CommonResult<Integer> postEntity(Payment payment){
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
        log.info(entity.getHeaders()+"-----"+entity.getHeaders()+"-----"+entity.getStatusCode()+"-----"+entity.getStatusCodeValue());
        if(entity.getStatusCode()== HttpStatus.OK){
            return entity.getBody();
        }else {
            return new CommonResult<>(400,"插入数据");
        }
    }
    @GetMapping("/consumer/payment/lb")
    public String lb(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        ServiceInstance instance = myloadBanlance.seviceInstance(instances);
        return restTemplate.getForObject(instance.getUri()+"/payment/lb",String.class);

    }

    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        ServiceInstance instance = myloadBanlance.seviceInstance(instances);
        return restTemplate.getForObject(instance.getUri()+"/payment/zipkin",String.class);
    }
}
